#include "ContrastEnhancementSettingsForm.h"
#include "ui_ContrastEnhancementSettingsForm.h"
#include <QDebug>

ContrastEnhancementSettingsForm::ContrastEnhancementSettingsForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ContrastEnhancementSettingsForm),
    m_enhancementStrength(50) // 默认增强强度为50%
{
    ui->setupUi(this);
    setAttribute(Qt::WA_StyledBackground, true);

    // 初始化滑块范围和初始值
    ui->sliderStrength->setMinimum(0);
    ui->sliderStrength->setMaximum(100);
    ui->sliderStrength->setValue(m_enhancementStrength);

    // 初始化输入框
    ui->editStrength->setText(QString::number(m_enhancementStrength) + QString("%"));

    // 连接信号和槽
    connect(ui->sliderStrength, SIGNAL(valueChanged(int)),
            this, SLOT(on_sliderStrength_valueChanged(int)));

    connect(ui->btnAutoEnhance, SIGNAL(clicked()),
            this, SLOT(on_btnAutoEnhance_clicked()));

    connect(ui->btnReset, SIGNAL(clicked()),
            this, SLOT(on_btnReset_clicked()));
}

ContrastEnhancementSettingsForm::~ContrastEnhancementSettingsForm()
{
    delete ui;
}

void ContrastEnhancementSettingsForm::setEnhancementStrength(int strength)
{
    // 确保强度在有效范围内
    m_enhancementStrength = qBound(0, strength, 100);

    // 更新UI控件
    ui->sliderStrength->setValue(m_enhancementStrength);
    ui->editStrength->setText(QString::number(m_enhancementStrength) + QString("%"));
}

int ContrastEnhancementSettingsForm::enhancementStrength() const
{
    return m_enhancementStrength;
}

void ContrastEnhancementSettingsForm::on_sliderStrength_valueChanged(int value)
{
    // 更新增强强度和输入框显示
    m_enhancementStrength = value;
    ui->editStrength->setText(QString::number(m_enhancementStrength) + QString("%"));

    // 发出变更信号
    emit enhancementStrengthChanged(m_enhancementStrength);
}

void ContrastEnhancementSettingsForm::on_btnAutoEnhance_clicked()
{
    // 发出自动增强信号，触发基于图像直方图的自动对比度增强
    emit autoEnhanceClicked();
}

void ContrastEnhancementSettingsForm::on_btnReset_clicked()
{
    // 复位增强强度为默认值50%
    setEnhancementStrength(50);

    // 发出变更信号
    emit enhancementStrengthChanged(m_enhancementStrength);
}
